package com.example.mq.server.diskdatamanager.database;

import com.example.mq.MqApplication;
import com.example.mq.server.virtualhost.Binding;
import com.example.mq.server.virtualhost.Exchange;
import com.example.mq.server.virtualhost.ExchangeType;
import com.example.mq.server.virtualhost.MQueue;

import java.io.File;
import java.util.List;

// 整合数据库操作
public class DataBaseManager {
    private MetaMapper metaMapper;

    private String rl = "./diskdata/meta.db";

    // 针对数据库进行初始化
    // 确保数据库和库中的表存在
    public void init() {

        // 手动的获取到 MetaMapper
        // 从 Spring 中拿到现成的对象
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        if (!checkDBExists()) {
            // 数据库不存在, 进行建表操作（自动建库）

            // 建表
            createTable();

            // 插入默认数据
            createDefaultData();

        } else {
            // 数据库已经存在
            System.out.println("[DataBaseManager] 数据库已检查完毕");
        }
    }

    public void deleteDB() {
        // 删文件
        File file = new File(rl);
        boolean ret = file.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库文件成功");
        } else {
            System.out.println("[DataBaseManager] 删除数据库文件失败");
        }

        // 删目录
        // 使用 delete 删除目录的时候, 需要保证目录是空的
        File dataDir = new File("./data");
        ret = dataDir.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库目录成功!");
        } else {
            System.out.println("[DataBaseManager] 删除数据库目录失败!");
        }
    }

    private boolean checkDBExists() {
        File file = new File(rl);
        if (file.exists()) {
            return true;
        }
        return false;
    }


    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
    }


    private void createDefaultData() {
        // 存在一个默认的匿名交换机，类型是 DIRECT
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 数据库已完成初始化");
    }

    // 封装数据库操作
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }
    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MQueue queue) {
        metaMapper.insertQueue(queue);
    }
    public List<MQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }
    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }
    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
}
